//
//  HomeDevice_tvCell.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/5.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "HomeDevice_tvCell.h"

@implementation HomeDevice_tvCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    self.bgV.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.bgV.layer.shadowRadius = 2;
    self.bgV.layer.shadowOffset = CGSizeMake(0, 2);
    self.bgV.layer.shadowOpacity = 0.3;
    self.bgV.layer.cornerRadius = 2;
    self.bgV.clipsToBounds = NO;
    
}
- (IBAction)name_btn_action:(UIButton *)sender {
    
    __weak __typeof(self) weakSelf = self;
    
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"设备重命名";
    [dialogViewController addTextFieldWithTitle:nil configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"请输入新名称";
        textField.text = weakSelf.entity.device_name;
        //        textField.maximumTextLength = 10;
    }];
    dialogViewController.shouldEnableSubmitButtonBlock = ^BOOL(__kindof QMUIDialogTextFieldViewController * _Nonnull aDialogViewController) {
        if ([aDialogViewController.textFields.firstObject.text isEqualToString:weakSelf.entity.name] || aDialogViewController.textFields.firstObject.text.length == 0) {
            return NO;
        }
        return YES;
        
    };
    //    dialogViewController.shouldManageTextFieldsReturnEventAutomatically = YES;// 让键盘的 Return 键也能触发确定按钮的事件。这个属性默认就是 YES，这里为写出来只是为了演示
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(__kindof QMUIDialogTextFieldViewController * _Nonnull dialogViewController) {
        
        NSString *text = dialogViewController.textFields.firstObject.text;
        //        [QMUITips showSucceed:@"提交成功" inView:self.view hideAfterDelay:1.2];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
        
        
        [dict setObject:app_code forKey:@"app_code"];
        [dict setObject:weakSelf.entity.device_id forKey:@"device_id"];
        [dict setObject:text forKey:@"name"];
        
        [PPHTTPRequest req:@"api/App/modifyDeviceInfo" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                [self.name_btn setAttributedTitle:[self attributedTextWithpicName:@"compile_icon" text:text iconfirst:NO] forState:UIControlStateNormal];

                if (self.blk) {
                    self.blk(text);
                }
            }
            
            else
                //            if ([error_code isEqualToString:@""])
            {
                
                [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
                
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:[error localizedDescription]];
            
        }];
        [dialogViewController hide];
    }];
    [dialogViewController show];

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setEntity:(App_devicesData *)entity{
    _entity = entity;
    [self.icon_iv sd_setImageWithURL:[NSURL URLWithString:entity.icon] placeholderImage:[UIImage imageNamed:@"baby_bed_img"]];
        
    [self.name_btn setAttributedTitle:[self attributedTextWithpicName:@"compile_icon" text:[entity.device_name stringByAppendingString:@"  "] iconfirst:NO] forState:UIControlStateNormal];
    CGSize fitSize = [self.name_btn sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.name_btn.frame))];
    self.name_btn.frame = CGRectMake(self.name_btn.frame.origin.x, self.name_btn.frame.origin.y, fmin(fitSize.width, ScreenWidth - 74 - 18 - 8), fitSize.height);
    
    if (entity.isonline == 1) {
        self.status_lb.text = @"运行中";
        self.status_lb.textColor = rgb(29,194,200);
        self.status_lb.backgroundColor = rgb(232,248,249);
    }else{
        
        self.status_lb.text = @"已离线";
        self.status_lb.textColor = rgb(255,119,57);
        self.status_lb.backgroundColor = rgb(253,234,225);
    }



}


- (NSMutableAttributedString *)attributedTextWithpicName:(NSString *)picName text:(NSString *)text iconfirst:(BOOL)iconFirst{
    // 添加表情
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    attch.image = [UIImage imageNamed:picName];
    
    // 设置图片大小
    CGFloat picH = 11;
    attch.bounds = CGRectMake(0, -(picH * LZYFitX - 11) , picH * LZYFitX, picH * LZYFitX);
    // 创建带有图片的富文本
    NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:attch];
    
    
    // 创建一个富文本
    NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", text]];
    
    // 修改富文本中的不同文字的样式
    [textStr addAttribute:NSForegroundColorAttributeName value:RGBA(51, 51, 51, 1) range:NSMakeRange(0, textStr.length)];
    [textStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(0, textStr.length)];
    
    NSMutableAttributedString *str  = [[NSMutableAttributedString alloc]init];

    if (iconFirst) {
        
        str  = [[NSMutableAttributedString alloc]initWithAttributedString:iconString];

        [str appendAttributedString:textStr];
    }else{
        
        str  = [[NSMutableAttributedString alloc]initWithAttributedString:textStr];

        [str appendAttributedString:iconString];
    }
   
    
    return str;
}
@end
